package com.itheima.leetcode;

import com.itheima.ArrayStack;

/**
 * 双栈模拟队列
 * 调用push、pop等方法的次数最多100
 *
 * @FileName E04LeetCode232
 * @Description
 * @Author Sword
 * @date 2025-05-08
 **/
public class E04LeetCode232 {

    /*
        队列头             队列尾
                          a    b
        顶   底           底   顶
        s1                      s2

        s2.push(a)
        s2.push(b)

        先把s2的所有元素移动到s1
        s1.pop

     */

    ArrayStack<Integer> s1 = new ArrayStack<>(100);
    ArrayStack<Integer> s2 = new ArrayStack<>(100);

    public void push(int x) {   //向队列尾添加
        s2.push(x);
    }

    public int pop() {  //向队列头移除
        if (s1.isEmpty()) {
            while (!s2.isEmpty()) {
                s1.push(s2.pop());
            }
        }
        return s1.pop();
    }

    public int peek() { //从队列头获取
        if (s1.isEmpty()) {
            while (!s2.isEmpty()) {
                s1.push(s2.pop());
            }
        }
        return s1.peek();
    }

    public boolean empty() {
        return s1.isEmpty() && s2.isEmpty();
    }

    public static void main(String[] args) {
        E04LeetCode232 queue = new E04LeetCode232();
        queue.push(1);
        queue.push(2);

        System.out.println(queue.pop());
        System.out.println(queue.pop());
    }
}
